/* 
 *  MHEG-5 Engine (ISO-13522-5)
 *  Copyright (C) 2007 Jason Leonard
 * 
 *  Work based on libmythfreemheg part of mythtv (www.mythtv.org)
 *  Copyright (C) 2004 David C. J. Matthews
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 *
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MHEG.Parser;

namespace MHEG.Actions
{
    class MHTransitionTo : MHElemAction
    {
        protected bool m_fIsTagged;
        protected int m_nConnectionTag;
        protected int m_nTransitionEffect;

        public MHTransitionTo()
            : base(":TransitionTo") 
        {
            m_fIsTagged = false;
            m_nConnectionTag = 0;
            m_nTransitionEffect = -1;
        }

        public override void Initialise(MHParseNode p, MHEngine engine)
        {
            base.Initialise(p, engine); // Target
            // The next one may be present but NULL in binary.
            if (p.GetArgCount() > 1) 
            {
                MHParseNode pCtag = p.GetArgN(1);
                if (pCtag.NodeType == MHParseNode.PNInt) 
                {
                    m_fIsTagged = true;
                    m_nConnectionTag = pCtag.GetIntValue();
                }
                else Logging.Assert(pCtag.NodeType == MHParseNode.PNNull);
            }
            if (p.GetArgCount() > 2) 
            {
                MHParseNode pTrEff = p.GetArgN(2);
                m_nTransitionEffect = pTrEff.GetIntValue();
            }            
        }

        public override void Perform(MHEngine engine)
        {
            MHObjectRef target = new MHObjectRef();
            m_Target.GetValue(target, engine); // Get the target
            engine.TransitionToScene(target);           
        }

        protected override void PrintArgs(TextWriter writer, int nTabs)
        {
            if (m_fIsTagged) writer.Write(" {0} ", m_nConnectionTag);
            else if (m_nTransitionEffect >= 0) writer.Write(" NULL ");
            if (m_nTransitionEffect >= 0) writer.Write(" {0}", m_nTransitionEffect);
        }
    }
}
